<template>
  <div class="mainos">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10">
        <el-form
          :inline="true"
          :rules="rules"
          :model="monitorForm"
          class="demo-form-inline marginTop0"
          ref="userListFrom"
          label-width="60px"
        >
          <div class="main_crumbs boxdq">
            <div class="main_crumbs_l">
              <a href="javascript:;" @click="$router.back(-1)">
                <i class="el-icon-arrow-down"></i>返回
              </a>
              <em>|</em>
              <span>对账</span>
            </div>
          </div>
          <div class="main_tile boxdq marginTop10">
            <div class="main_tile_l marginLeft0"><em></em>基础信息</div>
          </div>
          <div class="main_xq">
            <ul class="boxha">
              <li>
                原始单号：<span>{{ info.info.related_order_sn }}</span>
              </li>
              <li>
                配送日期：<span>{{
                  info.info.send_time
                    ? parseTime(info.info.related_time, "{y}-{m}-{d}")
                    : ""
                }}</span>
              </li>
              <li>
                用户名称：<span>{{ info.info.nickname }}</span>
              </li>
              <li class="boxnr">
                账单号：<span>{{ info.info.order_sn }}</span>
              </li>
              <li class="boxnr">
                下单金额：<span>{{ info.info.order_money }}</span>
              </li>
              <li class="boxnr">
                配送金额：<span>{{ info.info.send_money }}</span>
              </li>
              <li class="boxnr">
                对账金额：
                <span class="main_xqs">
                  <el-input
                    v-model.trim="total.dz"
                    placeholder="对账金额"
                    :disabled="true"
                    clearable
                  ></el-input>
                </span>
              </li>
              <li class="boxnr">
                差异金额：
                <span class="main_xqs">
                  <el-input
                    v-model.trim="total.ce"
                    placeholder="对账金额"
                    :disabled="true"
                    clearable
                  ></el-input>
                </span>
              </li>
              <li class="boxnr">
                运费：
                <span class="main_xqs">
                  <el-input-number
                    placeholder="运费"
                    v-model.trim="info.info.delivery_fee"
                    :min="0"
                    :precision="2"
                    :controls="false"
                    @change="isEdit = true"
                  ></el-input-number>
                </span>
              </li>
              <li class="boxnr">
                优惠金额：
                <span class="main_xqs">
                  <el-input-number
                    v-model.trim="info.info.discount_money"
                    placeholder="优惠金额"
                    :min="0"
                    :precision="2"
                    :controls="false"
                  ></el-input-number>
                </span>
              </li>
              <li class="main_w100 box">
                备注：
                <span class="main_xqs on" style="width: 90%; margin-top: -25px">
                  <el-form-item prop="remark" label-width="0" label=" ">
                    <el-input
                      v-model.trim="monitorForm.form.remark"
                      placeholder="备注"
                      clearable
                      @change="isEdit = true"
                    ></el-input>
                  </el-form-item>
                </span>
              </li>
            </ul>
          </div>
          <div class="main_tile boxdq marginTop0">
            <div class="main_tile_l marginLeft0"><em></em>商品详情</div>
            <!-- <div class="mainos_tile_r">合计：{{ total.dz }}元</div> -->
          </div>
          <div class="main_white marginBottom10">
            <div class="main_white">
              <div class="main_table marginTop0">
                <el-table
                  ref="multipleTable"
                  :data="monitorForm.list"
                  element-loading-text="加载中"
                  fit
                  border
                  highlight-current-row
                  show-summary
                  :summary-method="getSummaries"
                >
                  <el-table-column label="序号">
                    <template slot-scope="scope">{{
                      scope.$index + 1
                    }}</template>
                  </el-table-column>
                  <el-table-column label="商品名称" prop="name" width="150">
                    <template slot-scope="scope"
                      >{{ scope.row.goods_name }}【{{
                        scope.row.spu_name
                      }}】</template
                    >
                  </el-table-column>
                  <el-table-column label="单位" prop="unit" />
                  <el-table-column
                    label="原下单数量"
                    prop="order_num"
                    width="150"
                  />
                  <el-table-column
                    label="原下单单价"
                    prop="order_price"
                    width="150"
                    class-name="main_hei"
                  />
                  <el-table-column
                    label="原下单金额"
                    prop=""
                    width="150"
                    class-name="main_hei"
                  >
                    <template slot-scope="scope">{{
                      (parseFloat(scope.row.order_price) *
                        parseFloat(scope.row.order_num))
                        | twoPlace
                    }}</template>
                  </el-table-column>
                  <el-table-column
                    label="对账数量"
                    prop="account_num"
                    width="150"
                    class-name="main_hei"
                  >
                    <template slot-scope="scope">
                      <el-form-item
                        :prop="'list.' + scope.$index + '.account_num'"
                        :rules="rules.account_num"
                        label-width="10px"
                        label=" "
                      >
                        <el-input
                          v-model.trim="scope.row.account_num"
                          placeholder="对账数量"
                          class="wid110"
                          :disabled="
                            scope.row.activity_type == 4 ||
                            scope.row.activity_type == 5
                          "
                          @change="isEdit = true"
                          @input="(val) => inputchnage(val, scope, 0)"
                        ></el-input>
                      </el-form-item>
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="对账单价"
                    prop="account_price"
                    width="150"
                    class-name="main_hei"
                  >
                    <template slot-scope="scope">
                      <el-form-item
                        :prop="'list.' + scope.$index + '.account_price'"
                        :rules="rules.account_price"
                        label-width="10px"
                        label=" "
                      >
                        <el-input
                          v-model.trim="scope.row.account_price"
                          placeholder="对账单价"
                          class="wid110"
                          :disabled="
                            scope.row.activity_type == 4 ||
                            scope.row.activity_type == 5
                          "
                          @change="isEdit = true"
                          @input="(val) => inputchnage(val, scope, 1)"
                        ></el-input>
                      </el-form-item>
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="对账金额"
                    prop="account_money"
                    width="150"
                    class-name="main_hei"
                  >
                    <template slot-scope="scope">
                      <el-form-item
                        :prop="'list.' + scope.$index + '.account_money'"
                        :rules="rules.account_money"
                        label-width="10px"
                        label=" "
                      >
                        <el-input
                          v-model.trim="scope.row.account_money"
                          placeholder="对账金额"
                          class="wid110"
                          :disabled="
                            scope.row.activity_type == 4 ||
                            scope.row.activity_type == 5
                          "
                          @change="isEdit = true"
                          @input="(val) => inputchnage(val, scope, 2)"
                        ></el-input>
                      </el-form-item>
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="差异数量"
                    prop=""
                    width="120"
                    class-name="main_hei"
                  >
                    <template slot-scope="scope">
                      {{
                        parseFloat(scope.row.order_num) -
                        parseFloat(scope.row.account_num)
                          ? $toFixed(
                              parseFloat(scope.row.order_num) -
                                parseFloat(scope.row.account_num)
                            )
                          : "0.00"
                      }}
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="差异单价"
                    prop=""
                    width="120"
                    class-name="main_hei"
                  >
                    <template slot-scope="scope">
                      {{
                        parseFloat(scope.row.order_price) -
                        parseFloat(scope.row.account_price)
                          ? $toFixed(
                              parseFloat(scope.row.order_price) -
                                parseFloat(scope.row.account_price)
                            )
                          : "0.00"
                      }}
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="差异金额"
                    prop=""
                    width="120"
                    class-name="main_hei"
                  >
                    <template slot-scope="scope">
                      {{
                        $toFixed(
                          parseFloat(
                            $toFixed(
                              parseFloat(scope.row.order_price) *
                                parseFloat(scope.row.order_num)
                            )
                          ) - parseFloat(scope.row.account_money)
                        )
                      }}
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="备注"
                    prop="remark"
                    width="350"
                    class-name="main_hei"
                  >
                    <template slot-scope="scope">
                      <el-form-item label-width="0" label="">
                        <el-input
                          v-model.trim="scope.row.remark"
                          placeholder="备注"
                          class="wid240"
                        ></el-input>
                      </el-form-item>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </div>

          <div class="newConBox_p200 paddingBottom10" id="newConBox">
            <div class="newConBox_h32">
              <div
                :class="['mainor_pr boxdq newConBox ', { on: searchBarFixed }]"
              >
                <div class="mainor_prl">
                  <el-button
                    type="primary"
                    @click="preservation()"
                    v-repeatClick
                    :loading="loading"
                    >保存</el-button
                  >
                  <el-button @click="$router.back(-1)">取消</el-button>
                  <el-button
                    v-if="account_synchro_order == 1"
                    v-repeatClick
                    :loading="loading"
                    @click="preservation(1)"
                    >保存并修改原始订单<el-tooltip
                      class="item"
                      effect="dark"
                      placement="bottom"
                    >
                      <div slot="content">
                        注释：确定后会同步修改原始订单的配送数量、配送单价<br />
                        （配送金额小计=配送数量*配送单价）、订单配送总价等
                      </div>
                      <svg
                        aria-hidden="true"
                        class="navbar_svg new_navbar_svg"
                        width="14"
                        height="14"
                      >
                        <use xlink:href="#icon-summary_question" />
                      </svg> </el-tooltip
                  ></el-button>
                </div>
              </div>
            </div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import {
  statement_info,
  statement_account,
} from "@/api/financial/financialReceivables";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import rules from "@/rules/financial/financialReceivables_record";
import { parseTime } from "@/utils/index";

export default {
  computed: {
    total() {
      let dz = 0;
      let ce = 0;
      let list_dz = 0;
      this.monitorForm.list.forEach((e) => {
        dz += Number(this.$toFixed(parseFloat(e.account_money)));
        // ce +=
        //   parseFloat(e.account_money) -
        //   parseFloat(e.account_price) * parseFloat(e.account_num)
      });
      list_dz = this.$toFixed(dz);
      dz += parseFloat(this.info.info.delivery_fee);
      dz -= parseFloat(this.info.info.discount_money);
      ce = parseFloat(this.info.info.order_money) - dz;
      dz = dz ? this.$toFixed(dz) : "0.00";
      ce = ce ? this.$toFixed(ce) : "0.00";

      return { dz, ce, list_dz };
    },
  },

  components: {},

  data() {
    return {
      // 验证
      rules,
      // 时间
      parseTime,
      // 基本信息
      info: {
        info: {
          send_time: "2000-01-01",
          type: 0,
          status: 0,
          statement_list: [],
          remark: "",
        },
        statement_status_list: ["未分拣"],
        statusList: ["未分拣"],
      },
      listLoading: false,
      // 列表数据
      monitorForm: {
        form: {
          remark: "",
          delivery_fee: "",
          discount_money: "",
        },
        list: [],
      },
      // id
      id: this.$route.params.id,
      // 监听滚动
      searchBarFixed: false,
      isEdit: false,
      account_synchro_order: 0,
      loading: false,
    };
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  beforeRouteLeave(to, from, next) {
    if (this.isEdit) {
      setTimeout(() => {
        this.$confirm(
          "您已经有数据操作，离开页面系统不会保存您的操作，确定离开吗？",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(() => {
            // 正常跳转
            next();
          })
          .catch(() => {
            next(false);
          });
      }, 200);
    } else {
      next();
    }
  },
  created() {
    // 基本信息
    this.statement_info();
  },
  methods: {
    // 监听滚动
    handleScroll() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      scrollTop + document.documentElement.clientHeight >
      document.getElementById("newConBox").offsetTop + 300
        ? (this.searchBarFixed = false)
        : (this.searchBarFixed = true);
    },
    // 保存
    preservation(type) {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          if (type) {
            this.$confirm("保存后会同步修改原始订单的数据,是否继续?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {
                this.statement_account(type);
              })
              .catch(() => {});
          } else {
            this.statement_account(type);
          }
        } else {
        }
      });
    },
    statement_account(type) {
      let num = 0;
      let arry = [];
      this.monitorForm.form.delivery_fee = this.info.info.delivery_fee;
      this.monitorForm.form.discount_money = this.info.info.discount_money;
      let monitorForm = JSON.parse(JSON.stringify(this.monitorForm));

      monitorForm.list.map((e) => {
        arry.push({
          remark: e.remark,
          account_num: e.account_num,
          account_price: e.account_price,
          account_money: e.account_money,
          id: e.id,
        });
      });
      let data = Object.assign(
        apiUrl().name,
        monitorForm.form,
        {
          list: JSON.stringify(arry),
          id: this.id,
          status: type ? 1 : "",
        }
      );
      statement_account(data).then((res) => {
        if (res.errcode == 0) {
          this.$message({
            message: "保存成功",
            type: "success",
          });
          this.isEdit = false;
          this.$router.go(-1);
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 基本信息
    statement_info() {
      let data = Object.assign(
        apiUrl().name,
        { id: this.id }
      );
      statement_info(data).then((res) => {
        if (res.errcode == 0) {
          // res.data.info.statement_list.map((e) => {
          //   e.account_money = (
          //     parseFloat(e.account_price) * parseFloat(e.account_num)
          //   ).toFixed(2);
          // });
          this.account_synchro_order = res.data.account_synchro_order;
          this.monitorForm.list = res.data.info.statement_list;
          this.monitorForm.form.remark = res.data.info.remark;
          this.info = res.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 修改单价合数量
    inputchnage(val, scope, type) {
      if (!/^-?\d*\.?\d*$/.test(val)) {
        return;
      }
      if (type == 0 && scope.row.account_price && val) {
        scope.row.account_money = this.$toFixed(
          parseFloat(scope.row.account_price) *
            parseFloat(scope.row.account_num)
        );
      } else if (type == 1 && scope.row.account_num && val) {
        scope.row.account_money = this.$toFixed(
          parseFloat(scope.row.account_price) *
            parseFloat(scope.row.account_num)
        );
      } else if (type == 2) {
      }
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        switch (index) {
          case 0:
            sums[index] = "合计";
            break;

          case 8:
            sums[index] = this.$toFixed(parseFloat(this.total.list_dz));
            break;
        }
      });

      return sums;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainos {
  .main_tile {
    .mainos_tile_r {
      color: $colour_red;
      font-size: $font_14;
    }
  }
  .newConBox.on {
    margin-left: -10px;
  }
  .main_table {
    /deep/.el-table--small td {
      padding-top: 0;
    }
    /deep/.el-form-item--small.el-form-item {
      margin-bottom: 16px;
      margin-top: 16px;
      margin-right: 0;
    }
  }
  .main_xqs {
    width: 55%;
    display: block;
    text-align: left;
    /deep/.el-input--small {
      width: 100%;
    }
    &.on {
      /deep/.el-form-item--small.el-form-item {
        width: 100%;
        .el-form-item__content {
          width: 100%;
        }
      }
    }
  }
  .main_xq {
    ul {
      li {
        .main_xqi_i {
          position: relative;
          width: 120px;
          overflow: hidden;
          /deep/.el-upload-list__item-status-label {
            position: absolute;
            right: -15px;
            top: -6px;
            z-index: 91;
            width: 40px;
            height: 24px;
            background: #13ce66;
            text-align: center;
            -webkit-transform: rotate(45deg);
            transform: rotate(45deg);
            -webkit-box-shadow: 0 0 1pc 1px rgba(0, 0, 0, 0.2);
            box-shadow: 0 0 1pc 1px rgba(0, 0, 0, 0.2);
            display: block;
            i {
              font-size: $font_12;
              margin-top: 11px;
              color: $white;
              -webkit-transform: rotate(-45deg);
              transform: rotate(-45deg);
            }
          }
        }
        /deep/.el-image .el-image__inner {
          width: 120px;
          height: 120px;
          border: 1px solid #f8f8f9;
          margin-right: 20px;
        }
        /deep/.el-image .image-slot img {
          width: 120px;
          height: 120px;
          border: 1px solid #f8f8f9;
          margin-right: 20px;
        }
        .main_xqi {
          width: 80%;
          margin-left: 5px;
        }
        .main_upImg {
          /deep/ul {
            li {
              padding-left: 0;
            }
          }
          /deep/.el-upload-list--picture-card
            .el-upload-list__item:first-child {
            margin-left: 145px;
          }
        }
      }
    }
  }

  .mainor_pr {
    margin-left: 10px;
    margin-bottom: 10px;
  }
  .base_p {
    width: 80px;
  }
  .main_page_top {
    margin-top: 7px;
    margin-left: 10px;
    use {
      fill: $menuText;
    }
    &:hover {
      cursor: pointer;
      use {
        fill: $menuActiveText;
      }
    }
  }
}
.new_navbar_svg {
  vertical-align: bottom;
  margin-left: 5px;
}
</style>
